package org.feit.findword.view;

import java.util.ListIterator;

import org.feit.findword.R;
import org.feit.findword.game.Game;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class StatsView extends View {
	private static final String TAG = "StatsView";

	static int PADDING;

	private Game mGame;
	private Paint mPaint;
	private boolean mLoaded;

	public StatsView(Context context, Game game) {
		super(context);
		Log.d(TAG, "StatsView consturctor");
		mGame = game;
		initView();
	}

	public StatsView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView();
	}

	public StatsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();

	}

	private void initView() {
		mPaint = new Paint();
		mPaint.setTextAlign(Paint.Align.CENTER);
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(0);
		mPaint.setARGB(0xff, 0xff, 0xcc, 0xaa);
	}

	public void setGame(Game game) {
		mGame = game;
		mLoaded = true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int width = getWidth();
		int height = getHeight();
		PADDING = 4 * height / 100;
		float fontSize = height / 20;
		mPaint.setTextSize(fontSize);
		canvas.drawText(getContext().getString(R.string.stats), width / 2, PADDING * 3 / 2, mPaint);
		canvas.drawLine(PADDING, PADDING * 2, width - PADDING, PADDING * 2, mPaint);
		if (!mLoaded) {
			return;
		}
		int pos = PADDING * 2;
		mPaint.setTextSize(fontSize);
		ListIterator<String> words = mGame.guessedWords();
		int total = 0;
		while (words.hasNext()) {
			pos += fontSize;
			String w = words.next();
			mPaint.setTextAlign(Align.LEFT);
			canvas.drawText(String.format("%s (%d)", w, w.length()), PADDING,
					pos, mPaint);
			mPaint.setTextAlign(Align.RIGHT);
			int wp = Game.wordPoints(w.length());
			canvas.drawText(String.format("= %d", wp), width - PADDING, pos,
					mPaint);
			total += wp;
		}
		mPaint.setTextAlign(Align.LEFT);
		pos += fontSize;
		canvas.drawText(String.format("%d %s x 1",
				mGame.getTimeRemained(), getContext().getString(R.string.seconds)), PADDING, pos, mPaint);
		mPaint.setTextAlign(Align.RIGHT);
		canvas.drawText(
				String.format("= %d", mGame.getTimeRemained()), width
						- PADDING, pos, mPaint);
		pos += 4;
		canvas.drawLine(PADDING, pos, width - PADDING, pos, mPaint);
		pos += fontSize;
		int levelTotal = mGame.getLevelPoints() + mGame.getTimeRemained();
		canvas.drawText(String.format("%d", levelTotal), width
				- PADDING, pos, mPaint);
		if(mGame.getCurrentLevel() > 1) {
			pos += fontSize;
			canvas.drawText(String.format("+ %d", mGame.getPoints() - levelTotal), width
					- PADDING, pos, mPaint);
		}
		pos += fontSize;		
		mPaint.setARGB(0xff, 0xff, 0xff, 0xff);
		canvas.drawText(String.format("= %d", mGame.getPoints()), width
				- PADDING, pos, mPaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int side = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec
				.getSize(heightMeasureSpec));

		setMeasuredDimension(side, side);
	}

}
